/*
 * OREKIT-X
 * Copyright 2002-2008 CS Communication & Systemes
 * 
 * Parts of this software package have been licensed to CS
 * Communication & Systemes (CS) under one or more contributor license
 * agreements.  See the NOTICE file distributed with this work for
 * additional information.
 *  
 * This is an experimental copy of OREKIT from www.orekit.org.
 * Please use the original OREKIT from orekit.org for normal work
 * unrelated to this research project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License.  You
 * may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package ore.propagation.analytical;

import ore.Attitude;
import ore.AttitudeLaw;
import ore.Orbit;
import ore.SpacecraftState;
import ore.attitudes.InertialLaw;
import ore.errors.OrekitException;
import ore.errors.PropagationException;
import ore.orbits.EquinoctialOrbit;
import ore.propagation.AbstractPropagator;
import ore.time.AbsoluteDate;

/** 
 * Keplerian orbit propagator.
 * 
 * @author Guylaine Prat
 */
public class KeplerianPropagator
    extends AbstractPropagator
{

    /** 
     * Default mass.
     */
    private static final double DEFAULT_MASS = 1000.0;
    /** 
     * Default attitude law.
     */
    private static final AttitudeLaw DEFAULT_LAW = InertialLaw.EME2000_ALIGNED;

    /** 
     * Initial orbit.
     */
    private EquinoctialOrbit initialOrbit;
    /** 
     * Attitude law.
     */
    private final AttitudeLaw attitudeLaw;
    /** 
     * Central attraction coefficient (m^3/s^2).
     */
    private final double mu;
    /** 
     * Initial mass.
     */
    private double mass;
    /** 
     * Mean motion.
     */
    private double meanMotion;


    /** 
     * The central attraction coefficient &mu; is set to the same value used
     * for the initial orbit definition. Mass and attitude law are set to
     * unspecified non-null arbitrary values.
     * 
     * @param initialOrbit Initial orbit
     */
    public KeplerianPropagator(Orbit initialOrbit){
        this (initialOrbit, DEFAULT_LAW, initialOrbit.getMu(), DEFAULT_MASS);
    }
    /** 
     * Mass and attitude law are set to unspecified non-null arbitrary values.
     * 
     * @param initialOrbit Initial orbit
     * @param mu Central attraction coefficient (m^3/s^2)
     */
    public KeplerianPropagator(Orbit initialOrbit, double mu){
        this (initialOrbit, DEFAULT_LAW, mu, DEFAULT_MASS);
    }
    /** 
     * The central attraction coefficient &mu; is set to the same value
     * used for the initial orbit definition. Mass is set to an unspecified
     * non-null arbitrary value.
     * 
     * @param initialOrbit Initial orbit
     * @param attitudeLaw Attitude law
     */
    public KeplerianPropagator(Orbit initialOrbit, AttitudeLaw attitudeLaw){
        this (initialOrbit, attitudeLaw, initialOrbit.getMu(), DEFAULT_MASS);
    }
    /** 
     * Mass is set to an unspecified non-null arbitrary value.
     * 
     * @param initialOrbit Initial orbit
     * @param attitudeLaw Attitude law
     * @param mu Central attraction coefficient (m^3/s^2)
     */
    public KeplerianPropagator(Orbit initialOrbit, AttitudeLaw attitudeLaw, double mu){
        this (initialOrbit, attitudeLaw, mu, DEFAULT_MASS);
    }
    /** 
     * @param initialOrbit Initial orbit
     * @param attitudeLaw Attitude law
     * @param mu central Attraction coefficient (m^3/s^2)
     * @param mass Spacecraft mass (kg)
     */
    public KeplerianPropagator(Orbit initialOrbit, AttitudeLaw attitudeLaw,
                               double mu, double mass)
    {
        super();
        this.initialOrbit = new EquinoctialOrbit(initialOrbit);
        this.attitudeLaw  = attitudeLaw;
        this.mu           = mu;
        this.mass         = mass;
        {
            double a = initialOrbit.getA();
            this.meanMotion   = Math.sqrt(mu / a) / a;
        }
    }


    /**
     * {@inheritDoc}
     */
    protected AbsoluteDate getInitialDate() {
        return this.initialOrbit.getDate();
    }
    /**
     * {@inheritDoc}
     */
    protected SpacecraftState basicPropagate(final AbsoluteDate date)
        throws PropagationException
    {
        try {
            /*
             * Evaluation of LM = PA + RAAN + M at extrapolated time
             */
            final EquinoctialOrbit orbit =
                new EquinoctialOrbit(initialOrbit.getA(), 
                                     initialOrbit.getEquinoctialEx(),
                                     initialOrbit.getEquinoctialEy(), 
                                     initialOrbit.getHx(),
                                     initialOrbit.getHy(),
                                     (initialOrbit.getLM() + (meanMotion * date.durationFrom(initialOrbit.getDate()))),
                                     EquinoctialOrbit.MEAN_LATITUDE_ARGUMENT,
                                     initialOrbit.getFrame(), date, this.mu);
            /*
             * Evaluation of attitude
             */
            final Attitude attitude = attitudeLaw.getState(date,
                                                           orbit.getPositionVelocity(),
                                                           orbit.getFrame());

            return new SpacecraftState(orbit, attitude, this.mass);
        } 
        catch (OrekitException oe) {
            throw new PropagationException(oe.getMessage(), oe);
        }
    }
    /**
     * {@inheritDoc}
     */
    protected void resetInitialState(SpacecraftState state)
        throws PropagationException
    {
        this.initialOrbit   = new EquinoctialOrbit(state.getOrbit());
        final double a = initialOrbit.getA();
        this.meanMotion     = Math.sqrt(mu / a) / a;
        this.mass           = state.getMass();
    }
}
